package com.sumainfor.common.utlis;

import java.awt.*;

/**
 * 功能描述:
 * HTML相关工具类
 *
 * @Auther: zhlu
 * @Date: 2019/4/9 17:59
 * @Description:
 */
public class HtmlUtil {

    //默认没有关键字
    public static final String DEFAULT_BODY = "&lt;p&gt;%s&nbsp;%s&lt;/p&gt;";

    //默认颜色的span
    public static final String DEFAULT_BODY_SPAN_NOCOLOR = "&lt;span style='color:#549BE0'&gt;%s&lt;/span&gt;";

    //有颜色的span
    public static final String DEFAULT_BODY_SPAN_COLOR = "&lt;span style='color:%s'&gt;%s&lt;/span&gt;";

    /**
     * 换行符
     */
    public static final String BR = "&lt;br/&gt;";

    /**
     * 功能描述:
     * 字符串转HTML源代码
     * str:文本
     * body：关键字
     * color:颜色
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/9 18:08
     */
    public static String stringToHTML(String str, String body, Color color) {
        StringBuffer out = new StringBuffer();
        String strout;
        //判断关键字是否为空
        if (body == null || body.isEmpty()) {
            strout = String.format(DEFAULT_BODY, ConvertDateTime.getCurrentTimes(ConvertDateTime.FORMAT_FULL_DATETIME_EN4), str);
            out.append(strout);
        } else {

            //判断颜色是否为空
            if (color == null) {
                //把关键字替换成默认颜色的span
                strout = String.format(DEFAULT_BODY, ConvertDateTime.getCurrentTimes(ConvertDateTime.FORMAT_FULL_DATETIME_EN4), str.replace(body, String.format(DEFAULT_BODY_SPAN_NOCOLOR, body)));
                out.append(strout);
            } else {
                String colorStr = String.format("rgb(%d,%d,%d)", color.getRed(), color.getGreen(), color.getBlue());
                //把关键字替换成有颜色的span
                strout = String.format(DEFAULT_BODY, ConvertDateTime.getCurrentTimes(ConvertDateTime.FORMAT_FULL_DATETIME_EN4), str.replace(body, String.format(DEFAULT_BODY_SPAN_COLOR, colorStr, body)));
                out.append(strout);
            }
        }
        return out.toString();
    }

    public static void main(String[] args) {
        System.out.printf(stringToHTML("反正作为一个90后", "90", new Color(255, 166, 132)));
    }
}
